package com.five.community.controller;

import com.five.community.bean.*;
import com.five.community.bean.vo.*;
import com.five.community.service.AssetService;
import com.five.community.service.VillageService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;


import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/asset")
public class AssetController {
    @Autowired
    private AssetService assetService;
    @Autowired
    private VillageService villageService;
    //页面显示
   @RequestMapping("/listUi")
    public String listUi(@RequestParam(value = "pageNum",required = false,defaultValue = "1")Integer pageNum,
                         @RequestParam(value = "pageSize",required = false,defaultValue = "5")Integer pageSize,
                         Model model, ModelMap map,String name) {
       if (pageNum == null) {
           pageNum = 1;
       }
       if (name == ""||name == null) {
           try {
               List<AssetVo> pages = assetService.selectAllAssets(pageNum, pageSize);
               PageInfo<AssetVo> pageInfo = new PageInfo<>(pages);
               model.addAttribute("pageInfo", pageInfo);
               List<AssetVo> assets = assetService.selectAll();
               map.addAttribute("assets", assets);
           } catch (Exception e) {
               throw new RuntimeException(e);
           }
       } else {
           try {
               List<AssetVo> pages= assetService.selectAllByName(pageNum,pageSize,name);
               List<AssetVo> assets = assetService.selectByName(name);
               PageInfo<AssetVo> pageInfo = new PageInfo<>(pages);
               model.addAttribute("pageInfo",pageInfo);
               Asset asset = new Asset();
               asset.setName(name);
               map.addAttribute("asset",asset);
               map.addAttribute("assets",assets);
           } catch (Exception e) {
               throw new RuntimeException(e);
           }
       }
           return "asset";
   }
    //删除操作
    @RequestMapping("/delete")
    public String delete(long id){
        try {
            assetService.deleteById(id);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return "redirect:/asset/listUi";
    }
    //添加画面
    @RequestMapping("/addUi")
    public String addUi(ModelMap map1){
        try {
            List<Village> villages = villageService.selectAll();
            map1.addAttribute("villages",villages);

        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return "asset_add";
    }
    //添加方法
    @RequestMapping("/add")
    public String add(AssetVo assetVo){
        try {
            long vid = Long.parseLong(assetVo.getVname());
            assetVo.setCreateTime(new Date());
            Asset asset = new Asset(vid,assetVo.getEquipId(),assetVo.getName(),assetVo.getBrand(),assetVo.getPrice(),
                    assetVo.getNum(),assetVo.getBdate(),assetVo.getServiceLife(),new Date());
            assetService.insert(asset);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "redirect:/asset/addUi";
    }
    //搜索功能
    @RequestMapping("/select")
    public String select(@RequestParam(value = "pageNum",required = false,defaultValue = "1")Integer pageNum,
                         @RequestParam(value = "pageSize",required = false,defaultValue = "5")Integer pageSize,
                         String name,ModelMap map,Model model) {
        if (name == "") {
            return "redirect:/asset/listUi";
        } else {
            try {
                List<AssetVo> pages= assetService.selectAllByName(pageNum,pageSize,name);
                List<AssetVo> assets = assetService.selectByName(name);
                PageInfo<AssetVo> pageInfo = new PageInfo<>(pages);
                model.addAttribute("pageInfo",pageInfo);
                map.addAttribute("assets", assets);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
            return "asset";
        }
    }
    //更新页面
    @RequestMapping("/editUi")
    public String editUi(long id, ModelMap map1){
        try {
            Asset asset = assetService.selectById(id);
            List<Village> villages = villageService.selectAll();
            map1.addAttribute("villages",villages);
            map1.addAttribute("asset",asset);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return "asset_edit";
    }
    //更新方法
    @RequestMapping("/edit")
    public String edit(AssetVo assetVo) {
        long id1 = Long.parseLong(assetVo.getVname());
        assetVo.setCreateTime(new Date());
        try {
            Asset asset = new Asset(assetVo.getId(),id1,assetVo.getEquipId(),assetVo.getName(),assetVo.getBrand(),assetVo.getPrice(),
                    assetVo.getNum(),assetVo.getBdate(),assetVo.getServiceLife(),new Date());
            assetService.update(asset);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return "asset_edit";
    }
}
